/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 02.01.2006
 *
 *************************************************/
package fightplan.persistence;

import static fightplan.persistence.ImportHelper.getString;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import fightplan.business.Category;
import fightplan.business.Fighter;
import fightplan.business.Gender;
import fightplan.business.Tournament;
import fightplan.business.util.BeltUtil;
import fightplan.business.util.WeightUtil;

public class ExcelGenericImport {

	static void imprtFightersAndTeams(Tournament tournament, File file) {
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
			HSSFWorkbook wb = new HSSFWorkbook(fs);

			// read Fighters
			HSSFSheet sheet = wb.getSheetAt(0);
			List<Fighter> fighters = readFighters(tournament, sheet);

			// insert at end to avoid partially read
			tournament.getFighters().addAll(fighters);
		} catch (Exception x) {
			x.printStackTrace();
			JOptionPane.showMessageDialog(null, "Import fehlgeschlagen: " + x.getMessage(), "Fehler",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private static int headerRowCount(HSSFSheet sheet, String lastText) {
		int pos = 0;
		while (pos <= sheet.getLastRowNum()) {
			HSSFRow row = sheet.getRow(pos++);
			if (row == null || getString(row, 0) == null)
				continue;

			if (getString(row, 0).startsWith(lastText)) {
				return pos;
			}
		}
		return pos;
	}

	private static List<Fighter> readFighters(Tournament tournament, HSSFSheet sheet) {
		int pos = headerRowCount(sheet, "Name");
		String[] line = new String[11];
		List<Fighter> readFighters = new ArrayList<Fighter>();
		while (pos <= sheet.getLastRowNum()) {
			HSSFRow row = sheet.getRow(pos++);
			if (row == null)
				continue;

			for (int i = 0; i < line.length; i++) {
				line[i] = getString(row, i);
			}

			Fighter fighter = readFighter(tournament, line);
			if (fighter != null) {
				readFighters.add(fighter);
			}
		}
		return readFighters;
	}

	private static Fighter readFighter(Tournament tournament, String[] line) {
		if (line == null || line[0] == null || line[0].trim().length() == 0) {
			return null;
		}
		Fighter fighter = new Fighter();
		fighter.setName(line[0]);
		fighter.setGender(Gender.parse(line[1]));
		fighter.setYearFormatted(line[2]);
		fighter.setWeight(WeightUtil.parse(line[3]));
		fighter.setBelt(BeltUtil.parse(line[4]));
		fighter.setDojo(tournament.getOrCreateDojo(line[5]));
		if (line[6] != null && line[6].trim().length() > 0) {
			Category category = tournament.findCategory(fighter, true, false);
			if (category != null) {
				category.getFighters().add(fighter);
			}
		}
		if (line[7] != null && line[7].trim().length() > 0) {
			Category category = tournament.findCategory(fighter, false, false);
			if (category != null) {
				category.getFighters().add(fighter);
			}
		}
		/*
		 * if (line[8] != null && line[8].trim().length() > 0) { Category
		 * category = tournament.findCategory(fighter, true, true); if (category
		 * != null) { category.getFighters().add(fighter); } } if (line[9] !=
		 * null && line[9].trim().length() > 0) { Category category =
		 * tournament.findCategory(fighter, false, true); if (category != null)
		 * { category.getFighters().add(fighter); } }
		 */
		return fighter;
	}

}
